#!/bin/bash

OPENGAUSS_USER=opengauss
OPENGAUSS_GROUP=opengauss
PORT=54320

group_exist=$(cat /etc/group | grep -w $OPENGAUSS_GROUP)
if [ "$group_exist" == "" ]; then
    echo "group $OPENGAUSS_GROUP not exist. create group."
    groupadd $OPENGAUSS_GROUP
fi

user_exist=$(cat /etc/passwd | grep -w $OPENGAUSS_USER)
if [ "$user_exist" == "" ]; then
    echo "user $OPENGAUSS_USER not exist. create user."
    useradd -g $OPENGAUSS_GROUP -m $OPENGAUSS_USER
fi

datanode=/home/$OPENGAUSS_USER/datanode/dn_$RANDOM
su - $OPENGAUSS_USER -c "mkdir -p $datanode"

export GAUSSHOME=/usr/local/opengauss/1.1.0
echo "export GAUSSHOME=/usr/local/opengauss/1.1.0" >> /home/$OPENGAUSS_USER/.bashrc
echo "export LD_LIBRARY_PATH=/usr/local/opengauss/1.1.0/lib:$LD_LIBRARY_PATH" >> /home/$OPENGAUSS_USER/.bashrc
echo "export PATH=/usr/local/opengauss/1.1.0/bin:$PATH" >> /home/$OPENGAUSS_USER/.bashrc
su - $OPENGAUSS_USER -c "source /home/$OPENGAUSS_USER/.bashrc;gs_initdb -D $datanode -U $OPENGAUSS_USER -w openGauss@123 --nodename=single_node"
su - $OPENGAUSS_USER -c "source /home/$OPENGAUSS_USER/.bashrc;gs_guc set -D $datanode -c 'port=$PORT'"
su - $OPENGAUSS_USER -c "source /home/$OPENGAUSS_USER/.bashrc;gs_ctl start -D $datanode"
